const util = require("../utils/util")

/**
 * 成长卡下单
 * @param {Object} params 参数对象，以下为成员：
 * @member {Array} goods_info 商品信息
 * @member {String} goods_id goods_info数组成员:商品标识
 * @member {String} buy_number goods_info数组成员:购买数量
 * @member {String} spec_stock_id goods_info数组成员:选择规格
 * @member {String} price 价格
 * @member {String} coupon_id 优惠券标识
 * @member {Object} receive_info 收货人对象
 * @member {String} receive_mobile receive_info成员：收货人手机
 * @member {String} receive_name receive_info成员：收货人姓名
 * @member {String} receive_address receive_info成员：收货人地址
 * @member channel{String} 渠道
 * @member order_from {String} 下单操作来源: cart_info 购物车结算
 * 
 */
const transactions = function (params) {
  let url = util.getBaseUrl() + "/api/pay/transactions";
  let data = {
    token: wx.getStorageSync('token'), //util.getToken(),
    goods_info: params.goods_info,
    price: params.price,
    receive_info:params.receive_info,
    coupon_id:params.coupon_id,
    channel:params.channel?params.channel:'',
    is_share:params.is_share?params.is_share:'',
    order_from:params.order_from,
  };

  util.request({
    url: url,
    method: "POST",
    data: data,
    success(res) {
      // console.log("res", res)
      params.success(res)
    }
  })
}

/**
 * 发起支付
 * @param {Object} params {order_sn{String},pay_type{String},pay_client{String}}
 */
const payment = function (params) {
  let url = util.getBaseUrl() + "/api/pay/payment";
  let data = {
    token: wx.getStorageSync('token'),//util.getToken(),
    order_sn: params.order_sn,
    pay_type: params.pay_type,
    pay_client: params.pay_client,
    openid: wx.getStorageSync('openid'),//util.getOpenId(),
  };

  util.request({
    url: url,
    method: "POST",
    data: data,
    success(res) {
      // console.log("res", res)
      params.success(res)
    }
  })
}

  /**
   * 订单详情
   * @param {*} params 参数对象，成员如下：
   * @member order_sn {String} 
   */
   const getorderDetail = function (params) {
    let url = util.getBaseUrl() + "/api/user/orderDetail";
    let data = {
      token: util.getToken(),
      order_sn: params.order_sn,
    };

    util.request({
      url: url,
      method: "GET",
      data: data,
      success(res) {
        // console.log("res", res)
        params.success(res)
      }
    })
  }

module.exports = {
  transactions,
  payment,
  getorderDetail
}